/*
 * Copyright 2011-2016 YueJi.com All right reserved. This software is the confidential and proprietary information of
 * YueJi.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YueJi.com.
 */
package com.yue.commons.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;

/**
 * 日志工厂类，对LoggerFactory的包装
 * 
 * @author hanjie 2011-8-3 上午11:02:15
 */
public class SqlLoggerHandler {

    // 默认的慢sql时间
    private static final long DEFAULT_SLOWLY_TIME = 1;
    private long              slowtime            = DEFAULT_SLOWLY_TIME;
    private static Logger     logger              = LoggerFactory.getLogger(SqlLoggerHandler.class);

    public void record(String statement, Object[] args, long time) {
        // 如果是慢sql则记录完整的sql信息
        if (isSlowly(time)) {
            if (args == null) {
                logger.warn("ִ执行 {} ;耗时 {} ; 没有参数", statement, time);
            } else {
                logger.warn("ִ执行 {} ;耗时 {} ", statement, time);
                XStream stream = new XStream();
                String s = stream.toXML(args);
                logger.warn("参数信息:");
                logger.warn(s);
            }
        } else {
            // 不是慢sql，只记录statement及时间
            logger.debug("ִ执行 {} ;耗时 {} ;", statement, time);
        }
    }

    private boolean isSlowly(long time) {
        return time > slowtime;
    }

    public void setSlowtime(long slowtime) {
        this.slowtime = slowtime;
    }

}
